import React, { PureComponent } from 'react';
import axios from 'axios';
import { connect } from 'react-redux';
import { changeBannersAction, changeRecommendsAction } from '../store(拆分)/home';

//直接发送网络请求，将数据放入store中
class Category extends PureComponent {

  //class组件的componentDidMount中发送网络请求
  componentDidMount() {
    axios.get("http://123.207.32.32:8000/home/multidata").then(res => {
      const banners = res.data.data.banner.list;
      const recommends = res.data.data.recommend.list;
      this.props.changeBanners(banners);
      this.props.changeRecommends(recommends)
    })
  }

  render() {
    return ( 
      <div>
        <h2>CategoryPage</h2>
        <p>在这里请求数据, 放入store中</p>
      </div>
     );
  }
}

const mapDispatchToProps = (dispatch, ownProps) => {
  return {
    changeBanners: (banners) => {
      dispatch(changeBannersAction(banners))
    },
    changeRecommends: (recommends) => {
      dispatch(changeRecommendsAction(recommends))
    }
  }
}

export default connect(null, mapDispatchToProps)(Category);